import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
    // 首页
  { path:'',
    name:'Index',
    redirect:'/login',
    meta:{isLogin:true},
    component:()=>import('../views/Index')
  },
    // 登录页
  {
    path:'/login',
    name:'Login',
    meta:{isLogin:false},
    component:()=>import('../views/Login')
  },
  // 布局页
  {
    path:'/layout',
    name:'Layout',
    redirect: '/layout/role',
    meta:{isLogin:true},
    component:()=>import('../views/Layout'),
    //子路由
    children:[
      {path:'role', name:'Role',component:()=>import('../views/role/Role')},
      {path:'admin', name:'Admin',component:()=>import('../views/admin/Admin')},
      {path:'email', name:'Email',component:()=>import('../views/home/Email')},
      {path:'home', name:'Home',component:()=>import('../views/home/Home')},
      {path:'message', name:'Message',component:()=>import('../views/home/Message')},
      {path:'mine', name:'Mine',component:()=>import('../views/admin/Mine')},
      {path:'resetPwd', name:'ResetPwd',component:()=>import('../views/admin/ResetPwd')},
      {path:'roomType', name:'RoomType',component:()=>import('../views/roomType/RoomType')},
      {path:'room', name:'Room',component:()=>import('../views/room/Room')},
      {path:'guest', name:'Guest',component:()=>import('../views/guest/Guest')}
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//导入Nprogress
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

// 前置守卫
router.beforeEach((to,from,next)=>{
  Nprogress.start()
   //是否登录
  if (to.meta.isLogin){
    // 获取token
    const token=localStorage.getItem('token')
    if (token){
      next()
    }else {
      next('/login')
    }
  }else {
    next()
  }

})
// 后置守卫
router.afterEach((to,from)=>{
  Nprogress.done()
})
export default router
